package com.mouse.hxsj.view;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mouse.hxsj.dao.QqAccountMapper;
import com.mouse.hxsj.entity.po.QqAccount;
import com.mouse.hxsj.util.ViewGet;
import com.mouse.hxsj.util.ViewSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

@Slf4j
@Component
public class UserFrm extends JInternalFrame {
    private JTextField textField_account;
    private JTextField textField_description;
    private JTable table;
    private JTextField textField_num;
    private JTextField textField_desc1;
    private JButton button_delete;
    private JButton button_login;
    private JPanel panel_save;
    @Autowired
    private QqAccountMapper qqAccountMapper;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UserFrm frame = new UserFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UserFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("游戏用户");
        setBounds(20, 20, 700, 500);

        JPanel panel_search = new JPanel();
        panel_search.setBorder(new TitledBorder(null, "搜索条件", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JScrollPane scrollPane = new JScrollPane();

        panel_save = new JPanel();
        panel_save.setBorder(new TitledBorder(null, "新增", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(29)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(panel_save, GroupLayout.PREFERRED_SIZE, 611, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                                                .addComponent(panel_search, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE)))
                                .addContainerGap(44, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(20)
                                .addComponent(panel_search, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                                .addComponent(panel_save, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
                                .addGap(26))
        );

        JLabel label_1 = new JLabel("编号");

        textField_num = new JTextField();
        textField_num.setEditable(false);
        textField_num.setColumns(10);

        JLabel label_2 = new JLabel("账号");

        JLabel label_3 = new JLabel("密码");

        JLabel label_5 = new JLabel("备注");

        textField_desc1 = new JTextField();
        textField_desc1.setColumns(10);

        JButton button = new JButton("保存");
        button.addActionListener(e -> save());

        button_delete = new JButton("删除");
        button_delete.addActionListener(e -> delete());

        button_login = new JButton("隐藏密码");
        button_login.addActionListener(e -> hidePassword());

        textField = new JTextField();
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        GroupLayout gl_panel_1 = new GroupLayout(panel_save);
        gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_1)
                                                .addGap(16)
                                                .addComponent(textField_num, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addGap(27)
                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addGap(33)
                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                                .addGap(28)
                                                .addComponent(button_delete, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                                .addGap(26)
                                                .addComponent(button_login, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(textField_desc1)))
                                .addContainerGap(72, GroupLayout.PREFERRED_SIZE))
        );
        gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(textField_num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_2)
                                        .addComponent(label_3)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(30)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_5)
                                        .addComponent(textField_desc1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(button)
                                        .addComponent(button_delete)
                                        .addComponent(button_login))
                                .addContainerGap())
        );
        panel_save.setLayout(gl_panel_1);

        table = new JTable();
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent met) {
                tableSelected(met);
            }
        });
        table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "编号", "账号", "密码", "备注"
                }
        ));
        scrollPane.setViewportView(table);

        JLabel lblNewLabel = new JLabel("账号");

        textField_account = new JTextField();
        textField_account.setColumns(10);

        JLabel label = new JLabel("备注");

        textField_description = new JTextField();
        textField_description.setColumns(10);

        JButton btnNewButton = new JButton("搜索");
        btnNewButton.addActionListener(arg0 -> initTable());
        JButton btnNewButton_1 = new JButton("新增");
        btnNewButton_1.addActionListener(e -> addRole());

        JButton button_clear = new JButton("清空");
        button_clear.addActionListener(e -> {
            textField_account.setText("");
            textField_description.setText("");
            initTable();
        });
        GroupLayout gl_panel = new GroupLayout(panel_search);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(lblNewLabel)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(textField_account, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                .addGap(27)
                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(textField_description, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addComponent(btnNewButton)
                                .addGap(26)
                                .addComponent(button_clear)
                                .addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                                .addComponent(btnNewButton_1)
                                .addGap(38))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField_account, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label)
                                        .addComponent(textField_description, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(button_clear))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panel_search.setLayout(gl_panel);
        getContentPane().setLayout(groupLayout);
    }

    private void hidePassword() {
        if ("隐藏密码".equals(button_login.getText())) {
            button_login.setText("显示密码");
        } else {
            button_login.setText("隐藏密码");
        }
        initTable();
    }

    private void delete() {
        Integer id = ViewGet.textField(textField_num, Integer.class);
        if (id == null) return;
        int result = JOptionPane.showConfirmDialog(null, "是否删除id为" + id + "数据");
        if (result != 0) return;
        if (id != null) qqAccountMapper.deleteById(id);
        initTable();
    }

    private void save() {
        QqAccount qqAccount = new QqAccount();
        Integer id = ViewGet.textField(textField_num, Integer.class);
        qqAccount.setId(id);
        qqAccount.setUserName(ViewGet.textField(textField));
        qqAccount.setPassword(ViewGet.textField(textField_1));
        qqAccount.setDescription(ViewGet.textField(textField_desc1));
        if (id == null) {
            qqAccountMapper.insert(qqAccount);
        } else {
            qqAccountMapper.updateById(qqAccount);
        }
        initTable();
    }

    private void addRole() {
        clear();
        addViewFlag(true);
    }

    public void initFrm() {
        initTable();
        addViewFlag(true);
    }

    private void tableSelected(MouseEvent met) {
        addViewFlag(false);

        int row = table.getSelectedRow();
        Integer roleId = (Integer) table.getValueAt(row, 0);
        QqAccount qqAccount = qqAccountMapper.selectById(roleId);
        ViewSet.textField(textField_num, roleId);
        ViewSet.textField(textField_desc1, qqAccount.getDescription());
        ViewSet.textField(textField, qqAccount.getUserName());
        ViewSet.textField(textField_1, qqAccount.getPassword());
    }

    public void initTable() {
        addViewFlag(true);
        clear();
        if (qqAccountMapper == null) return;
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);
        List<QqAccount> list = qqAccountMapper.selectList(new LambdaQueryWrapper<QqAccount>()
                .like(QqAccount::getUserName, ViewGet.textField(textField_account))
                .like(QqAccount::getDescription, ViewGet.textField(textField_description)));
        for (QqAccount qqAccount : list) {
            Vector vector = new Vector();
            vector.add(qqAccount.getId());
            vector.add(qqAccount.getUserName());
            if ("隐藏密码".equals(button_login.getText())) {
                vector.add("******");
            } else {
                vector.add(qqAccount.getPassword());
            }
            vector.add(qqAccount.getDescription());
            tableModel.addRow(vector);
        }
    }

    private void clear() {
        ViewSet.textField(textField_num, "");
        ViewSet.textField(textField_desc1, "");
        ViewSet.textField(textField, "");
        ViewSet.textField(textField_1, "");
    }

    private void addViewFlag(boolean flag) {
        if (flag) {
//            button_delete.setVisible(false);
//            button_login.setVisible(false);
            panel_save.setBorder(new TitledBorder(null, "新增", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        } else {
//            button_delete.setVisible(true);
//            button_login.setVisible(true);
            panel_save.setBorder(new TitledBorder(null, "编辑", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        }
    }
}
